//
//  SystemNotionViewController.m
//  WULIHH
//
//  Created by tang on 16/5/10.
//  Copyright © 2016年 tang. All rights reserved.
//

#import "SystemNotionViewController.h"
#import "Header.h"
#import "UserInfoModel.h"
#import "WuLiRequestNotionAPI.h"


#import "ShowDetalImageViewController.h"
#import "AboutUSViewController.h"

@interface SystemNotionViewController ()<UITableViewDelegate, UITableViewDataSource>
{
    UITableView *mainTableView;
    NSMutableArray *dataArray;
    
    NSString *pageString;
}



@end

@implementation SystemNotionViewController


- (void)loadView
{
    [super loadView];
    
    dataArray = [NSMutableArray new];
    pageString = @"1";
    
    UIView *navigationView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 64)];
    [navigationView setBackgroundColor:[UIColor colorWithRed:242.0 /255.0 green:202.0 /255.0 blue:48.0/255.0 alpha:1]];
    [self.view addSubview:navigationView];
    
    UIImageView *leftimageView = [[UIImageView alloc] initWithFrame:CGRectMake(8, 29, 10, 20)];
    //    [leftimageView setBackgroundColor:[UIColor redColor]];
    [leftimageView setImage:[UIImage imageNamed:@"Detalback"]];
    [navigationView addSubview:leftimageView];
    
    UILabel *namelabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 29, WithView, 20)];
    [namelabel setText:@"系统通知"];
    [namelabel setTextAlignment:NSTextAlignmentCenter];
    [namelabel setFont:[UIFont systemFontOfSize:16.0]];
    [navigationView addSubview:namelabel];
    
    UILabel *setUpLabel = [[UILabel alloc] initWithFrame:CGRectMake(23, 27, 33, 24)];
    [setUpLabel setText:@"返回"];
    [setUpLabel setTextAlignment:NSTextAlignmentCenter];
    [setUpLabel setFont:[UIFont systemFontOfSize:15.0]];
    [setUpLabel setTextColor:[UIColor blackColor]];
    [self.view addSubview:setUpLabel];
    
    UIButton *leftbtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 25, 60, 60)];
    [leftbtn addTarget:self action:@selector(leftEvent) forControlEvents:UIControlEventTouchUpInside];
    [navigationView addSubview:leftbtn];

    mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64.0, WithView, heightView-64.0)];
    [self.view addSubview:mainTableView];
    
    [WuLiRequestNotionAPI getSystemNotion:systemNotion withPage:pageString];
    [self.tabBarController setHidesBottomBarWhenPushed:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [mainTableView setDataSource:self];
    [mainTableView setDelegate:self];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notionGetBack:) name:messageString object:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (void)leftEvent
{
    [self.navigationController popViewControllerAnimated:YES];
}




//消息返回
- (void)notionGetBack:(NSNotification *)notion
{
    NSDictionary *dic = [notion userInfo];
    NSLog(@"%@", dic);
    if ([[dic valueForKey:@"success"] intValue] == 1)
    {
        if ([pageString isEqualToString:@"1"])
        {
            [dataArray removeAllObjects];
            
        }
        else
        {
            
        }
        [dataArray addObjectsFromArray:[dic valueForKey:@"data"]];
        [mainTableView reloadData];
    }
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return dataArray.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellInden = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellInden];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellInden];
    }
    [cell.textLabel setText:[dataArray[indexPath.row] valueForKey:@"content"]];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *typeInt = [dataArray[indexPath.row] valueForKey:@"msgType"];
    if ([typeInt isEqualToString:@"work"])
    {
                ShowDetalImageViewController *detalView = [[ShowDetalImageViewController alloc] init];
                detalView.IDString = [NSString stringWithFormat:@"%@", [dataArray[indexPath.row] valueForKey:@"id"]];
                detalView.textStr = @"作品详情";
                [self.navigationController pushViewController:detalView animated:YES];

    }
    else if ([typeInt isEqualToString:@"link"])  //链接
    {
        AboutUSViewController *aboutVC = [[AboutUSViewController alloc] init];
        aboutVC.urlString = [NSString stringWithFormat:@"%@", [dataArray[indexPath.row] valueForKey:@"linkUrl"]];
        [self.navigationController pushViewController:aboutVC animated:YES];
    }
    else if ([typeInt isEqualToString:@"text"])  //作品
    {
    }

}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
